/**
 * 该场景固定长宽:
 *   1280 x 720
 * 
 * HTML 设置方案
 * #main
 *   #ribbons (Ribbons 的 root element)
 */

import { Panel } from '../comps/fallen_knight/panel';
import { Lottery } from '../comps/fallen_knight/lottery';

function createScene(context) {
	let t = new App(context);
	let scene = new context.Scene({
		require : ['zrender'],
		init : (libs) => t.init(libs),
		destroy : () => t.destroy(),
		collectInfo : () => t.collectInfo(),
		onMessage : (key, data) => t.onMessage(key, data),
		onTick : (assetId, data) => t.onTick(assetId, data),
	});
	t.sendStatus = scene.sendStatus;
	return scene;
}

/**
 * 简易版弹幕机
 * 
 * 流程:
 * 1. monitor 需要添加告诉屏幕, 房间号
 * 2. 屏幕这里来创建资源、开启资源、绑定资源
 */

class App {
  roomId = 0;
  danmaku = null;

  /** @type {Panel} */
  panel = null;

  constructor(context) {
    this.context = context;
    this.lottery = new Lottery({ context });
  }

  init(libs) {
    // 先要创建 Dom
    const main = document.getElementById('main');
    const div = document.createElement('div');
    div.id = 'zrender';
    div.style.position = 'absolute';
    div.style.overflow = 'hidden';
    div.style.width = '1280px';
    div.style.height = '720px';
    div.style.left = '0px';
    div.style.top = '0px';
    main.appendChild(div);
    const zr = this.zr = libs.zrender.init(div);

    this.panel = new Panel({
      zrender: libs.zrender,
      zr,
      context: this.context,
      lottery: this.lottery,
		});

		const roomStr = this.context.parseInitParam().room;
		if (roomStr) {
			const roomId = parseInt(roomStr);
			if (roomId) {
				this.handleAsset(roomId);
			}
    }
    
    this.panel._init();
    
    // test
    global.app = this;
  }

  destroy() {
    if (this.zr) {
      this.zr.dispose();
    }
    let dom = document.getElementById('zrender');
    if (dom) {
      dom.remove();
    }
  }
  
  collectInfo() {
    return {
      roomId : this.roomId,
      any : this.lottery.any,
      words : this.lottery.words,
      phase : this.lottery.phase,
      chosen : this.lottery.chosen,
      participants : this.lottery.participants.length,
    };
  }
  
  onMessage(key, data) {
    switch (key) {
      case '/load/room': { // 修改房间号
        this.handleAsset(data.roomId);
        this.sendStatus();
      } return;
      case '/load/words': { // 修改文字
        this.lottery.updateWords(data.words);
        this.sendStatus();
      } return;
      case '/lottery/reset': { // 重置
        this.lottery.reset();
        this.sendStatus();
      } return;
      case '/lottery/begin': { // 开始弹幕起
        this.lottery.begin();
        this.sendStatus();
      } return;
      case '/lottery/end': { // 弹幕不再接收
        this.lottery.end();
        this.sendStatus();
      } return;
      case '/lottery/ready': { // 开始抽
        this.lottery.ready();
        this.sendStatus();
      } return;
      case '/lottery/pick': { // 抽取人
        this.lottery.picking();
        this.sendStatus();
      } return;
    }
    
    console.log("danmaku/fallen_knight 收到无法解析的命令: " + key, data);
  }

  sendStatus() {
    // 等待被覆盖
  }

  onTick(assetId, data) {
    if (this.danmaku != null) {
      this.danmaku.filterAssetTick(assetId, data);
    }
  }

  /**
   * 创建资源、绑定资源
	 * @param {number} roomId B 站房间号, 例如 7734200
   */
  handleAsset(roomId) {
    this.roomId = roomId;

    this.danmaku = new this.context.Danmaku(roomId);
    this.danmaku.createSource()
      .then(() => this.danmaku.openSource())
      .then(() => {
        this.danmaku.onDanmuReceived = (o) => this.lottery.onDanmuReceived(o);
      });
  }
}

export default createScene;